home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2010 April / PCA177.iso / ESSENTIALS / Firefox Setup.exe / nonlocalized / chrome / browser.jar / content / browser / pageinfo / security.js < prev   
Encoding:
JavaScript  |  2009-07-15  |  11.1 KB  |  341 lines

  1. //@line 40 "e:\builds\moz2_slave\win32_build\build\browser\base\content\pageinfo\security.js"
  2.  
  3. var security = {
  4.   // Display the server certificate (static)
  5.   viewCert : function () {
  6.     var cert = security._cert;
  7.     viewCertHelper(window, cert);
  8.   },
  9.  
  10.   _getSecurityInfo : function() {
  11.     const nsIX509Cert = Components.interfaces.nsIX509Cert;
  12.     const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  13.     const nsX509CertDB = "@mozilla.org/security/x509certdb;1";
  14.     const nsISSLStatusProvider = Components.interfaces.nsISSLStatusProvider;
  15.     const nsISSLStatus = Components.interfaces.nsISSLStatus;
  16.  
  17.     // We don't have separate info for a frame, return null until further notice
  18.     // (see bug 138479)
  19.     if (gWindow != gWindow.top)
  20.       return null;
  21.  
  22.     var hName = null;
  23.     try {
  24.       hName = gWindow.location.host;
  25.     }
  26.     catch (exception) { }
  27.  
  28.     var ui = security._getSecurityUI();
  29.     if (!ui)
  30.       return null;
  31.  
  32.     var isBroken =
  33.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IS_BROKEN);
  34.     var isInsecure = 
  35.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IS_INSECURE);
  36.     var isEV =
  37.       (ui.state & Components.interfaces.nsIWebProgressListener.STATE_IDENTITY_EV_TOPLEVEL);
  38.     ui.QueryInterface(nsISSLStatusProvider);
  39.     var status = ui.SSLStatus;
  40.  
  41.     if (!isInsecure && status) {
  42.       status.QueryInterface(nsISSLStatus);
  43.       var cert = status.serverCert;
  44.       var issuerName =
  45.         this.mapIssuerOrganization(cert.issuerOrganization) || cert.issuerName;
  46.  
  47.       var retval = {
  48.         hostName : hName,
  49.         cAName : issuerName,
  50.         encryptionAlgorithm : undefined,
  51.         encryptionStrength : undefined,
  52.         isBroken : isBroken,
  53.         isEV : isEV,
  54.         cert : cert,
  55.         fullLocation : gWindow.location
  56.       };
  57.  
  58.       try {
  59.         retval.encryptionAlgorithm = status.cipherName;
  60.         retval.encryptionStrength = status.secretKeyLength;
  61.       }
  62.       catch (e) {
  63.       }
  64.  
  65.       return retval;
  66.     } else {
  67.       return {
  68.         hostName : hName,
  69.         cAName : "",
  70.         encryptionAlgorithm : "",
  71.         encryptionStrength : 0,
  72.         isBroken : isBroken,
  73.         isEV : isEV,
  74.         cert : null,
  75.         fullLocation : gWindow.location        
  76.       };
  77.     }
  78.   },
  79.  
  80.   // Find the secureBrowserUI object (if present)
  81.   _getSecurityUI : function() {
  82.     if (window.opener.gBrowser)
  83.       return window.opener.gBrowser.securityUI;
  84.     return null;
  85.   },
  86.  
  87.   // Interface for mapping a certificate issuer organization to
  88.   // the value to be displayed.
  89.   // Bug 82017 - this implementation should be moved to pipnss C++ code
  90.   mapIssuerOrganization: function(name) {
  91.     if (!name) return null;
  92.  
  93.     if (name == "RSA Data Security, Inc.") return "Verisign, Inc.";
  94.  
  95.     // No mapping required
  96.     return name;
  97.   },
  98.   
  99.   /**
  100.    * Open the cookie manager window
  101.    */
  102.   viewCookies : function()
  103.   {
  104.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  105.                        .getService(Components.interfaces.nsIWindowMediator);
  106.     var win = wm.getMostRecentWindow("Browser:Cookies");
  107.     var eTLDService = Components.classes["@mozilla.org/network/effective-tld-service;1"].
  108.                       getService(Components.interfaces.nsIEffectiveTLDService);
  109.  
  110.     var eTLD;
  111.     var uri = gDocument.documentURIObject;
  112.     try {
  113.       eTLD = eTLDService.getBaseDomain(uri);
  114.     }
  115.     catch (e) {
  116.       // getBaseDomain will fail if the host is an IP address or is empty
  117.       eTLD = uri.asciiHost;
  118.     }
  119.  
  120.     if (win) {
  121.       win.gCookiesWindow.setFilter(eTLD);
  122.       win.focus();
  123.     }
  124.     else
  125.       window.openDialog("chrome://browser/content/preferences/cookies.xul",
  126.                         "Browser:Cookies", "", {filterString : eTLD});
  127.   },
  128.   
  129.   /**
  130.    * Open the login manager window
  131.    */
  132.   viewPasswords : function()
  133.   {
  134.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  135.                        .getService(Components.interfaces.nsIWindowMediator);
  136.     var win = wm.getMostRecentWindow("Toolkit:PasswordManager");
  137.     if (win) {
  138.       win.setFilter(this._getSecurityInfo().hostName);
  139.       win.focus();
  140.     }
  141.     else
  142.       window.openDialog("chrome://passwordmgr/content/passwordManager.xul",
  143.                         "Toolkit:PasswordManager", "", 
  144.                         {filterString : this._getSecurityInfo().hostName});
  145.   },
  146.  
  147.   _cert : null
  148. };
  149.  
  150. function securityOnLoad() {
  151.   var info = security._getSecurityInfo();
  152.   if (!info) {
  153.     document.getElementById("securityTab").hidden = true;
  154.     document.getElementById("securityBox").collapsed = true;
  155.     return;
  156.   }
  157.   else {
  158.     document.getElementById("securityTab").hidden = false;
  159.     document.getElementById("securityBox").collapsed = false;
  160.   }
  161.  
  162.   const pageInfoBundle = document.getElementById("pageinfobundle");
  163.  
  164.   /* Set Identity section text */
  165.   setText("security-identity-domain-value", info.hostName);
  166.   
  167.   var owner, verifier, generalPageIdentityString;
  168.   if (info.cert && !info.isBroken) {
  169.     // Try to pull out meaningful values.  Technically these fields are optional
  170.     // so we'll employ fallbacks where appropriate.  The EV spec states that Org
  171.     // fields must be specified for subject and issuer so that case is simpler.
  172.     if (info.isEV) {
  173.       owner = info.cert.organization;
  174.       verifier = security.mapIssuerOrganization(info.cAName);
  175.       generalPageIdentityString = pageInfoBundle.getFormattedString("generalSiteIdentity",
  176.                                                                     [owner, verifier]);
  177.     }
  178.     else {
  179.       // Technically, a non-EV cert might specify an owner in the O field or not,
  180.       // depending on the CA's issuing policies.  However we don't have any programmatic
  181.       // way to tell those apart, and no policy way to establish which organization
  182.       // vetting standards are good enough (that's what EV is for) so we default to
  183.       // treating these certs as domain-validated only.
  184.       owner = pageInfoBundle.getString("securityNoOwner");
  185.       verifier = security.mapIssuerOrganization(info.cAName ||
  186.                                                 info.cert.issuerCommonName ||
  187.                                                 info.cert.issuerName);
  188.       generalPageIdentityString = owner;
  189.     }
  190.   }
  191.   else {
  192.     // We don't have valid identity credentials.
  193.     owner = pageInfoBundle.getString("securityNoOwner");
  194.     verifier = pageInfoBundle.getString("notset");
  195.     generalPageIdentityString = owner;
  196.   }
  197.  
  198.   setText("security-identity-owner-value", owner);
  199.   setText("security-identity-verifier-value", verifier);
  200.   setText("general-security-identity", generalPageIdentityString);
  201.  
  202.   /* Manage the View Cert button*/
  203.   var viewCert = document.getElementById("security-view-cert");
  204.   if (info.cert) {
  205.     security._cert = info.cert;
  206.     viewCert.collapsed = false;
  207.   }
  208.   else
  209.     viewCert.collapsed = true;
  210.  
  211.   /* Set Privacy & History section text */
  212.   var yesStr = pageInfoBundle.getString("yes");
  213.   var noStr = pageInfoBundle.getString("no");
  214.  
  215.   var uri = gDocument.documentURIObject;
  216.   setText("security-privacy-cookies-value",
  217.           hostHasCookies(uri) ? yesStr : noStr);
  218.   setText("security-privacy-passwords-value",
  219.           realmHasPasswords(uri) ? yesStr : noStr);
  220.   
  221.   var visitCount = previousVisitCount(info.hostName);
  222.   if(visitCount > 1) {
  223.     setText("security-privacy-history-value",
  224.             pageInfoBundle.getFormattedString("securityNVisits", [visitCount.toLocaleString()]));
  225.   }
  226.   else if (visitCount == 1) {
  227.     setText("security-privacy-history-value",
  228.             pageInfoBundle.getString("securityOneVisit"));
  229.   }
  230.   else {
  231.     setText("security-privacy-history-value", noStr);        
  232.   }
  233.  
  234.   /* Set the Technical Detail section messages */
  235.   const pkiBundle = document.getElementById("pkiBundle");
  236.   var hdr;
  237.   var msg1;
  238.   var msg2;
  239.  
  240.   if (info.isBroken) {
  241.     hdr = pkiBundle.getString("pageInfo_MixedContent");
  242.     msg1 = pkiBundle.getString("pageInfo_Privacy_Mixed1");
  243.     msg2 = pkiBundle.getString("pageInfo_Privacy_None2");
  244.   }
  245.   else if (info.encryptionStrength >= 90) {
  246.     hdr = pkiBundle.getFormattedString("pageInfo_StrongEncryption",
  247.                                        [info.encryptionAlgorithm, info.encryptionStrength + ""]);
  248.     msg1 = pkiBundle.getString("pageInfo_Privacy_Strong1");
  249.     msg2 = pkiBundle.getString("pageInfo_Privacy_Strong2");
  250.     security._cert = info.cert;
  251.   }
  252.   else if (info.encryptionStrength > 0) {
  253.     hdr  = pkiBundle.getFormattedString("pageInfo_WeakEncryption",
  254.                                         [info.encryptionAlgorithm, info.encryptionStrength + ""]);
  255.     msg1 = pkiBundle.getFormattedString("pageInfo_Privacy_Weak1", [info.hostName]);
  256.     msg2 = pkiBundle.getString("pageInfo_Privacy_Weak2");
  257.   }
  258.   else {
  259.     hdr = pkiBundle.getString("pageInfo_NoEncryption");
  260.     if (info.hostName != null)
  261.       msg1 = pkiBundle.getFormattedString("pageInfo_Privacy_None1", [info.hostName]);
  262.     else
  263.       msg1 = pkiBundle.getString("pageInfo_Privacy_None3");
  264.     msg2 = pkiBundle.getString("pageInfo_Privacy_None2");
  265.   }
  266.   setText("security-technical-shortform", hdr);
  267.   setText("security-technical-longform1", msg1);
  268.   setText("security-technical-longform2", msg2); 
  269.   setText("general-security-privacy", hdr);
  270. }
  271.  
  272. function setText(id, value)
  273. {
  274.   var element = document.getElementById(id);
  275.   if (!element)
  276.     return;
  277.   if (element.localName == "textbox" || element.localName == "label")
  278.     element.value = value;
  279.   else {
  280.     if (element.hasChildNodes())
  281.       element.removeChild(element.firstChild);
  282.     var textNode = document.createTextNode(value);
  283.     element.appendChild(textNode);
  284.   }
  285. }
  286.  
  287. function viewCertHelper(parent, cert)
  288. {
  289.   if (!cert)
  290.     return;
  291.  
  292.   var cd = Components.classes[CERTIFICATEDIALOGS_CONTRACTID].getService(nsICertificateDialogs);
  293.   cd.viewCert(parent, cert);
  294. }
  295.  
  296. /**
  297.  * Return true iff we have cookies for uri
  298.  */
  299. function hostHasCookies(uri) {
  300.   var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"]
  301.                                 .getService(Components.interfaces.nsICookieManager2);
  302.  
  303.   return cookieManager.countCookiesFromHost(uri.asciiHost) > 0;
  304. }
  305.  
  306. /**
  307.  * Return true iff realm (proto://host:port) (extracted from uri) has
  308.  * saved passwords
  309.  */
  310. function realmHasPasswords(uri) {
  311.   var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
  312.                                   .getService(Components.interfaces.nsILoginManager);
  313.   return passwordManager.countLogins(uri.prePath, "", "") > 0;
  314. }
  315.  
  316. /**
  317.  * Return the number of previous visits recorded for host before today.
  318.  *
  319.  * @param host - the domain name to look for in history
  320.  */
  321. function previousVisitCount(host, endTimeReference) {
  322.   if (!host)
  323.     return false;
  324.   
  325.   var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
  326.                                  .getService(Components.interfaces.nsINavHistoryService);
  327.     
  328.   var options = historyService.getNewQueryOptions();
  329.   options.resultType = options.RESULTS_AS_VISIT;
  330.   
  331.   // Search for visits to this host before today
  332.   var query = historyService.getNewQuery();
  333.   query.endTimeReference = query.TIME_RELATIVE_TODAY;
  334.   query.endTime = 0;
  335.   query.domain = host;
  336.  
  337.   var result = historyService.executeQuery(query, options);
  338.   result.root.containerOpen = true;
  339.   return result.root.childCount;
  340. }
  341.